<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
  import { patientListApi, removePatientApi } from '@/apis/patient'
  import { useConsultStore } from '@/stores/consult'
  import type { PatientItem } from '@/types/patient'
  import { onPageShow } from '@dcloudio/uni-app'
  import { ref } from 'vue'

  // 侧滑删除按钮配置
  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
      },
    },
  ])

  // 列表数据
  const patinetList = ref<PatientItem[]>([])
  // 获取列表数据
  const getPatientList = async () => {
    const res = await patientListApi()
    console.log('患者列表', res)
    patinetList.value = res
    // 高亮默认患者
    const defaultPatient = patinetList.value.find((item) => item.defaultFlag === 1)
    if (defaultPatient) {
      // 保存默认患者
      store.patientId = defaultPatient.id
    }
  }

  //
  const activeId = ref('')
  // 点击删除患者
  const onDeleteItem = async (id: string) => {
    // 保存id
    activeId.value = id
    const res = await uni.showModal({
      title: '温馨提示',
      content: '确定删除该患者吗？',
    })
    // 清除id
    activeId.value = ''
    if (res.confirm) {
      // 发送请求让后端删除患者信息
      await removePatientApi(id)
      // 本地删除
      patinetList.value = patinetList.value.filter((item) => item.id !== id)
      // 轻提示
      uni.showToast({ title: '删除成功', icon: 'none' })
      // 清除 store 中的患者 id
      if (store.patientId === id) {
        store.patientId = ''
      }
    }
  }

  // 页面显示时刷新数据，因为新增后会返回列表页，所以要刷新数据
  onPageShow(() => {
    getPatientList()
  })

  const pageParams = defineProps<{
    type?: string
  }>()

  // 动态设置标题
  uni.setNavigationBarTitle({ title: pageParams.type === 'consult' ? '选择患者' : '档案列表' })

  const store = useConsultStore()
  // 选择患者
  const selectPatient = (id: string) => {
    if (pageParams.type === 'consult') {
      store.patientId = id
    }
  }

  // 去支付页
  const gotoPayment = () => {
    uni.navigateTo({ url: '/subpkg_consult/payment/index' })
  }
  // ---------------- 选择患者 ----------------
</script>

<template>
  <scroll-view>
    <view class="archive-page">
      <view v-if="pageParams.type === 'consult'">
        <view>请选择患者信息</view>
        <view class="archive-tips">以便医生给出更准确的治疗，信息仅医生可见</view>
      </view>

      <view v-else class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="item in patinetList"
          :key="item.id"
          :right-options="swipeOptions"
          @click="onDeleteItem(item.id)"
          :show="activeId === item.id ? 'right' : 'none'"
        >
          <view
            class="archive-card"
            @click="selectPatient(item.id)"
            :class="{ active: pageParams.type === 'consult' && store.patientId === item.id }"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{
                item.idCard.replace(/^(.{6}).+(.{4})$/, '$1*********$2')
              }}</text>
              <!-- 嵌套的 default -->
              <text v-if="item.defaultFlag === 1" class="default">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.gender === 1 ? '男' : '女' }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${item.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patinetList.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/index">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
      <button
        @click="gotoPayment"
        v-if="pageParams.type === 'consult'"
        :disabled="!store.patientId"
        class="uni-button"
      >
        下一步
      </button>
    </view>
  </scroll-view>
</template>

<style lang="scss">
  @import './index.scss';
</style>
